package com.toplabs.unashu.common.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

/**
 * @author toplabs
 * @since 2022/08/09
 * <p>This java file was created by toplabs in 2022/08/09.
 * The following is the description information about this file:</p>
 * <p>description: 统一返回格式</p>
 */
@Getter
@Builder
@AllArgsConstructor
public class CommonResponse<T> implements StatusCode {
    private int code;
    private String message;
    private T data;

    private CommonResponse() {
    }

    public static <T> CommonResponse<T> success() {
        return CommonResponse.<T>builder().code(ResponseStatus.SUCCESS.getCode()).message(ResponseStatus.SUCCESS.getMessage()).build();
    }

    public static <T> CommonResponse<T> success(String message) {
        return CommonResponse.<T>builder().code(ResponseStatus.SUCCESS.getCode()).message(message).build();
    }

    public static <T> CommonResponse<T> success(T data) {
        return CommonResponse.<T>builder().code(ResponseStatus.SUCCESS.getCode()).message(ResponseStatus.SUCCESS.getMessage()).data(data).build();
    }

    public static <T> CommonResponse<T> success(String message, T data) {
        return CommonResponse.<T>builder().code(ResponseStatus.SUCCESS.getCode()).message(message).data(data).build();
    }

    public static <T> CommonResponse<T> failed(StatusCode errorCode) {
        return CommonResponse.<T>builder().code(errorCode.getCode()).message(errorCode.getMessage()).build();
    }

    public static <T> CommonResponse<T> failed(String message) {
        return CommonResponse.<T>builder().code(ResponseStatus.FAILED.getCode()).message(message).build();
    }

    public static <T> CommonResponse<T> failed() {
        return failed(ResponseStatus.FAILED);
    }

    public static <T> CommonResponse<T> validateFailed() {
        return failed(ResponseStatus.VALIDATE_FAILED);
    }

    public static <T> CommonResponse<T> validateFailed(String message) {
        return CommonResponse.<T>builder().code(ResponseStatus.VALIDATE_FAILED.getCode()).message(message).build();
    }

    public static <T> CommonResponse<T> unauthorized(T data) {
        return CommonResponse.<T>builder().code(ResponseStatus.INVALID_TOKEN.getCode()).message(ResponseStatus.INVALID_TOKEN.getMessage()).data(data).build();
    }

    public static <T> CommonResponse<T> forbidden(T data) {
        return CommonResponse.<T>builder().code(ResponseStatus.FORBIDDEN.getCode()).message(ResponseStatus.FORBIDDEN.getMessage()).data(data).build();
    }

    public static <T> CommonResponse<T> result(int code, String message) {
        return CommonResponse.<T>builder().code(code).message(message).build();
    }

    public static <T> CommonResponse<T> result(StatusCode statusCode) {
        return CommonResponse.<T>builder().code(statusCode.getCode()).message(statusCode.getMessage()).build();
    }

    public static <T> CommonResponse<T> result(int code, String message, T data) {
        return CommonResponse.<T>builder().code(code).message(message).data(data).build();
    }

    public static <T> CommonResponse<T> result(StatusCode statusCode, T data) {
        return CommonResponse.<T>builder().code(statusCode.getCode()).message(statusCode.getMessage()).data(data).build();
    }
}